package com.wanli.storemobile.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.wanli.storemobile.R;
import com.wanli.storemobile.base.DataCallBack;
import com.wanli.storemobile.bean.BaseResponseBean;
import com.wanli.storemobile.homepage.model.HomePageModelImpl;
import com.wanli.storemobile.utils.ToastUtil;
import com.wanli.storemobile.widget.ViewLoading;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Author:HWC
 */
public class AddClassifyDialog extends DialogFragment {

    @BindView(R.id.tv_classify_name)
    EditText tvClassifyName;
    @BindView(R.id.tv_classify_sort)
    EditText tvClassifySort;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private Activity mActivity;
    private Dialog dialog;
    private String type;
    private String id;
    private String name;
    private String serial_number;
    private OnCallBackListener onCallBackListener;

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_add_classify, null);
        ButterKnife.bind(this, view);
        mActivity = getActivity();
        initView();
        return view;
    }


    public static AddClassifyDialog newInstance(String type, String id, String name, String serial_number) {
        Bundle args = new Bundle();
        AddClassifyDialog fragment = new AddClassifyDialog();
        args.putString("type", type);
        args.putString("id", id);
        args.putString("name", name);
        args.putString("serial_number", serial_number);
        fragment.setArguments(args);
        return fragment;
    }

    //    初始化请求
    private void initView() {
        type = getArguments().getString("type");
        id = getArguments().getString("id");
        name = getArguments().getString("name");
        serial_number = getArguments().getString("serial_number");
        if ("1".equals(type)) {
            tvTitle.setText("新增分类");
        } else if ("2".equals(type)) {
            tvTitle.setText("编辑分类");
            tvClassifyName.setText(name);
            tvClassifySort.setText(serial_number);
        }

    }

    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.getAttributes().windowAnimations = R.style.commomDialog;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick(R.id.tv_commit)
    public void onViewClicked() {
        String name = tvClassifyName.getText().toString();
        String sort = tvClassifySort.getText().toString();

        if (TextUtils.isEmpty(name)) {
            ToastUtil.showShort("请输入类别名称");
            return;
        }
        if (TextUtils.isEmpty(sort)) {
            ToastUtil.showShort("请输入类别排序");
            return;
        }
        ViewLoading.showProgress(mActivity);
        if ("1".equals(type)) {
            new HomePageModelImpl().requestStoreGoodsCategoryAdd(name, sort, new DataCallBack<BaseResponseBean>() {
                @Override
                public void onSuccessful(BaseResponseBean bean) {
                    onCallBackListener.onCallBack();
                    ToastUtil.showShort("新增成功");
                    dismiss();
                }

                @Override
                public void onFailed(String msg, String code) {

                }
            });
        } else if ("2".equals(type)) {
            new HomePageModelImpl().requestStoreGoodsCategoryEdit(id, name, sort, new DataCallBack<BaseResponseBean>() {
                @Override
                public void onSuccessful(BaseResponseBean bean) {
                    onCallBackListener.onCallBack();
                    ToastUtil.showShort("编辑成功");
                    dismiss();
                }

                @Override
                public void onFailed(String msg, String code) {

                }
            });
        }


    }

    public interface OnCallBackListener {
        void onCallBack();
    }
}
